﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RaisingStudio.Data.Providers;
using RaisingStudio.Data.Entities;

using System.Linq;

namespace SmartDeviceProject3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.Load +=new EventHandler(Form1_Load);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataProvider dataProvider = CreateDataProvider();
            object maxIssueID = dataProvider.GetMaxData<Issue>(Issue._.IssueID);
            if ((maxIssueID != null) && (!(maxIssueID is DBNull)))
            {
                Program.IssueID_Conuter = Convert.ToInt32(maxIssueID);
            }

            NewIssue(null, EventArgs.Empty);
        }

        private void NewIssue(object sender, EventArgs e)
        {
                Issue issue = new Issue();
                // no autoincrement.
                issue.IssueID = ++Program.IssueID_Conuter;
                #region issue properties.
                issue.Title = "test";
                issue.Status = IssueStatus.Fixed;
                    issue.IssueType = IssueType.Issue;
                issue.Owner ="me";

                    issue.Progress = 10.34M;
  
                issue.CreateTime = System.DateTime.Now;

                    issue.Locked = true;

                issue.Description = "desc";
                #endregion
                DataProvider dataProvider = CreateDataProvider();
                dataProvider.InsertDataObject<Issue>(issue);

                LoadIssueData();
        }

        private void SaveIssues(object sender, EventArgs e)
        {
            DataProvider dataProvider = CreateDataProvider();
            dataProvider.Update(typeof(Issue), this.dataGrid1.DataSource as DataTable);
        }

        private void DeleteIssue(object sender, EventArgs e)
        {
            DataProvider dataProvider = CreateDataProvider();

            dataProvider.Delete<Issue>(new object[] { Program.IssueID_Conuter });
            LoadIssueData();

        }


        public virtual DataProvider CreateDataProvider()
        {
            DataProvider dataProvider = new DataProvider();
            dataProvider.Log = Console.Out;
            return dataProvider;
        }

        private void LoadIssueData()
        {
            DataProvider dataProvider = CreateDataProvider();
            this.dataGrid1.DataSource = dataProvider.GetDataTable(typeof(Issue));
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadIssueData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DeleteIssue(null, EventArgs.Empty);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            DataTable dataTable = this.dataGrid1.DataSource as DataTable;
            dataTable.Rows[0][(string)Issue._.Title] = string.Format("Test from now: {0}", DateTime.Now);
            SaveIssues(sender, e);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            List<string> sa = new List<string>(new string[] { "a", "b", "c" });
            var q = from s in sa
                    where s == "b"
                    select s;
            foreach (var s in q)
            {
                Console.WriteLine(s);
            }
        }
    }
}